//
//  ZBLoginViewController.m
//  BaseRACDemo
//
//  Created by 庄波 on 2018/3/27.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "ZBLoginViewController.h"
#import "ZBLoginViewModel.h"

@interface ZBLoginViewController ()
@property (nonatomic,strong) ZBLoginViewModel *viewModel;

@end

@implementation ZBLoginViewController

-(instancetype)initWithViewModel:(ZBLoginViewModel *)viewModel{
    self = [super initWithViewModel:viewModel];
    if (self) {
        self.viewModel = viewModel;
    }
    return self;
}

- (void)bindViewModel{
    [super bindViewModel];
    
    //点击获取验证码
    [[self.btnGetCode rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (![self.textPhone.text isMobileNumber]) {
//            [CustomMBHUD showErrorWithStatus:@"手机号不正确"];
            FFToast *toast = [[FFToast alloc]initToastWithTitle:@"标题" message:@"消息内容......." iconImage:[UIImage imageNamed:@"fftoast_info"]];
            toast.toastPosition = FFToastPositionBelowStatusBarWithFillet;
            toast.toastBackgroundColor = [UIColor colorWithRed:75.f/255.f green:107.f/255.f blue:122.f/255.f alpha:1.f];
            [toast show:^{
                //点击消息通知时调用
            }];//[toast show];;
            return ;
        }
        [self.viewModel.btnGetCodeCommand execute:@1];
    }];
    
    //title绑定timer    
    [self.viewModel.timerSubject subscribeNext:^(id  _Nullable x) {
        self.btnGetCode.userInteractionEnabled = [x isEqualToString:@"获取验证码"];
        [self.btnGetCode setTitle:x forState:UIControlStateNormal];
    }];
    //绑定
    RAC(self.viewModel,phoneStr) = [RACSignal merge:@[RACObserve(self.textPhone, text),self.textPhone.rac_textSignal]];
    
    
    [[self.btnLogin rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        //请求登陆
        kWeakSelf(self);
        if ([NSString isNilOrEmpty:weakself.textCode.text]) {
            [CustomMBHUD showErrorWithStatus:@"请填写验证码"];
            return ;
        }
        [self.viewModel.btnLoginCommand execute:@[self.textPhone.text,self.textCode.text]];
    }];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setup];
}

- (void)setup{
    //背景色
    self.view.backgroundColor = KUIColorARGBString(@"#48ADE8");
    
    __block UIButton *btnPhone = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnPhone setImage:[UIImage imageNamed:@"shoujitubiao"] forState:UIControlStateNormal];
    [btnPhone setImage:[UIImage imageNamed:@"shoujitubiao"] forState:UIControlStateSelected];
    btnPhone.frame = CGRectMake(0, 0, 30, 30);
    self.textPhone.leftView = btnPhone;
    self.textPhone.leftViewMode = UITextFieldViewModeAlways;
    
    __block UIButton *btnPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnPassword setImage:[UIImage imageNamed:@"shuruyanzhegnma"] forState:UIControlStateNormal];
    [btnPassword setImage:[UIImage imageNamed:@"shuruyanzhegnma"] forState:UIControlStateSelected];
    btnPassword.frame = CGRectMake(0, 0, 30, 30);
    self.textCode.leftView = btnPassword;
    self.textCode.leftViewMode = UITextFieldViewModeAlways;
    
    //圆角设置
    self.textCode.layer.cornerRadius = 5;
    self.textPhone.layer.cornerRadius = 5;
    self.btnLogin.layer.cornerRadius = 5;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
